﻿using System;
using System.Web.UI.WebControls;
using System.Collections.Generic;
using System.Web.UI;
using System.Reflection;

using Developmentalmadness.Web.UI.AjaxAbstractions;

namespace Developmentalmadness.Web.UI.ExtJsControlLibrary
{
    public abstract class ExtJsComponent : ExtJsObservable
    {
        public override IList<ScriptDescriptor> GetScriptDescriptors()
        {
            IList<ScriptDescriptor> list = base.GetScriptDescriptors();

            ScriptControlDescriptorBase desc = (ScriptControlDescriptorBase)list[0];

            desc.Type = "Ext.Component";

            String[] properties = new String[]{
                "allowDomMove", "autoShow", "cls", "ctCls", "disabledClass",
                "hideMode", "hideParent", "plugins", "stateEvents",
                "stateId", "stateful", "style", "xtype"
            };

            Type component = typeof(ExtJsComponent);

            foreach (String property in properties)
            {
                PropertyInfo i = component.GetProperty(property);

                if (i == null)
                    throw new InvalidOperationException(String.Format("Property not found, 'ExtJsComponent.{0}'", property));

                Object value = i.GetValue(this, null);

                if (value == null)
                {
                    continue;
                }

                if (!(value is String))
                    desc.AddPropertyBase(property, value);
                else
                    desc.AddPropertyBase(property, String.Format("'{0}'", value));
            }

            //renderTo
            if (!String.IsNullOrEmpty(renderTo))
            {
                /* not sure if an exception should be thrown if ScriptManager is null (means this
                 control is a child of an 'items' collection). renderTo doesn't seem to work in this
                 scenario. An exception would make accidental use of this property in this situation
                 easier to troubleshoot. */
                if (String.Compare(renderTo, ID, true) == 0)
                    desc.AddPropertyBase("renderTo", String.Format("'{0}'", ClientID));
                else
                    desc.AddPropertyBase("renderTo", String.Format("'{0}'", GetControlID(renderTo)));
            }

            return list;
        }

        public Boolean? allowDomMove { get; set; }

        public Boolean? autoShow { get; set; }

        public String cls { get; set; }

        public String ctCls { get; set; }

        public Style disabledClass { get; set; }

        public hideMode? hideMode { get; set; }

        public Boolean? hideParent { get; set; }

        public String plugins { get; set; }

        public String renderTo { get; set; }

        public String stateEvents { get; set; }

        public String stateId { get; set; }

        public Boolean? stateful { get; set; }

        public Style style { get; set; }

        public xtype? xtype { get; set; }
    }
}
